package com.zzty.txj.controller;


import com.github.pagehelper.PageHelper;
import com.zzty.txj.entity.Question;
import com.zzty.txj.service.QuestionService;
import com.zzty.txj.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    QuestionService questionServiceImpl;

    @ResponseBody
    @RequestMapping(value="/insertTo",method = RequestMethod.POST)
    public Question insertTo(Question question, ModelMap model){

        System.out.println("question = "+question);
        boolean result = questionServiceImpl.add(question);
        Question question1 =new Question();
        if(result==true){
            int id = questionServiceImpl.getId();
            question1.setContent(question.getContent());
            question1.setId(id);
            model.addAttribute("question",question1);
            System.out.println("question1 = "+question1);
        }
       return  question1;
    }
    @ResponseBody
    @RequestMapping(value="/getQuestions")
    public List<Question> getQuestions(){
      List<Question> questions = questionServiceImpl.getQuestions();
        return  questions;
    }
@ResponseBody
@RequestMapping(value="/getPage")
    public List<Question> findUserModelByPage(int currentPage, int pageSize) {
    System.out.println("currentPage==="+currentPage);
    System.out.println("pageSize==="+pageSize);
        List<Question> questions = questionServiceImpl.findUserModelByPage(Integer.valueOf(currentPage), pageSize);
         System.out.println("questions1  "+questions);
        return questions ;
    }

    @ResponseBody
    @RequestMapping(value="/getCount")
    public Integer getCount(){
        Integer count = questionServiceImpl.getCount();
        System.out.println("count==="+count);
        return count ;
    }

}
